package com.snail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.common.DeleteRequest;
import com.snail.mapper.BizCourseOutlinesMapper;
import com.snail.mapper.BizCoursesBaseMapper;
import com.snail.model.dto.CoursePageListDto;
import com.snail.model.pojo.BizCoursesBase;
import com.snail.response.BaseResponse;
import com.snail.response.PageResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizCoursesBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author Monica
* @description 针对表【biz_courses_base(课程基本信息表)】的数据库操作Service实现
* @createDate 2025-07-17 13:16:38
*/
@Service
public class BizCoursesBaseServiceImpl extends ServiceImpl<BizCoursesBaseMapper, BizCoursesBase>
    implements BizCoursesBaseService {
    @Autowired
    private  BizCoursesBaseMapper bizCoursesBaseMapper;
    @Autowired
    private BizCourseOutlinesMapper bizCourseOutlinesMapper;

    @Override
    public PageResponse<BizCoursesBase> getCoursePageList(CoursePageListDto coursePageListDto) {
        Page<BizCoursesBase> basePage = new Page<>(coursePageListDto.getCurrent(), coursePageListDto.getPageSize());
        LambdaQueryWrapper<BizCoursesBase> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(coursePageListDto.getTitle())) {
            queryWrapper.like(BizCoursesBase::getTitle, coursePageListDto.getTitle());
        }
        if (coursePageListDto.getCategoryId() != null) {
            queryWrapper.eq(BizCoursesBase::getCategoryId, coursePageListDto.getCategoryId());
        }
        queryWrapper.orderByDesc(BizCoursesBase::getId);
        Page<BizCoursesBase> bizCoursesBasePage = bizCoursesBaseMapper.selectPage(basePage, queryWrapper);
        return ResultUtils.pageSuccess(bizCoursesBasePage);
    }

    @Override
    public BaseResponse addCourseBase(BizCoursesBase bizCoursesBase) {
        boolean save = save(bizCoursesBase);
        if (!save) {
            return ResultUtils.error("添加失败");
        }
        return ResultUtils.success();
    }

    @Override
    public BaseResponse updateCourseBase(BizCoursesBase bizCoursesBase) {
        boolean update = updateById(bizCoursesBase);
        if (!update) {
            return ResultUtils.error("更新失败");
        }
        return ResultUtils.success();
    }

    @Override
    public BaseResponse deleteCourseBase(DeleteRequest deleteRequest) {
        boolean remove = removeById(deleteRequest.getId());
        if (!remove) {
            return ResultUtils.error("删除失败");
        }
        return ResultUtils.success();
    }

}




